import { get } from '../../../api/request'

Page({
  data: {
    id: null,
    detail: null,
    loading: true
  },

  onLoad(options) {
    if (options.id) {
      this.setData({
        id: options.id
      })
      this.getReservationDetail(options.id)
    } else {
      wx.showToast({
        title: '参数错误',
        icon: 'none'
      })
      setTimeout(() => {
        wx.navigateBack()
      }, 1500)
    }
  },

  // 获取预约详情
  getReservationDetail(id) {
    this.setData({ loading: true })
    // 调用获取预约详情API
    get(`/user/reservation_info?r_id=${id}`).then(res => {
      console.log('我的志愿', res);

      if (res.code === 200) {
        let data = res.data || {};

        // 格式化状态文本和样式
        let statusText = data.status_txt || '未知状态';
        let statusClass = '';

        switch (data.status) {
          case 0:
            statusClass = 'status-pending';
            break;
          case 1:
            statusClass = 'status-confirmed';
            break;
          case 2:
            statusClass = 'status-completed';
            break;
          case 3:
            statusClass = 'status-cancelled';
            break;
          default:
            statusClass = 'status-unknown';
        }

        // 格式化数据
        const detail = {
          id: data.id,
          appoint_code: data.appoint_code,
          title: `预约编号：${data.appoint_code}`,
          appointment: data.appointment,
          user_name: data.user_name,
          gender: data.user_gender === 0 ? '男' : '女',
          subjects: data.subjects === 1 ? '理科' : '文科',
          grades: data.grades || '未填写',
          province_name: data.province_name,
          city_name: data.city_name,
          phone: data.phone,
          created_at: data.created_at,
          status: data.status,
          status_txt: statusText,
          statusClass: statusClass,
          order_sn: data.order_sn
        };

        this.setData({
          detail,
          loading: false
        });
      } else {
        wx.showToast({
          title: res.msg || '获取数据失败',
          icon: 'none'
        });
        this.setData({ loading: false });
      }
    }).catch(err => {
      console.error('获取预约详情错误:', err);
      wx.showToast({
        title: '网络错误，请稍后重试',
        icon: 'none'
      });
      this.setData({ loading: false });
    });
  },

  // 分享
  onShareAppMessage() {
    const { detail } = this.data
    return {
      title: detail?.title || '预约详情',
      path: `/pages/tianbao/detail/detail?id=${this.data.id}`
    }
  }
}) 